package com.moxi.mogublog.picture.config;

import com.alibaba.fastjson.JSONObject;
import com.moxi.mogublog.commons.entity.OnlineAdmin;
import com.moxi.mogublog.picture.global.RedisConf;
import com.moxi.mogublog.picture.global.SysConf;
import com.moxi.mogublog.utils.JsonUtils;
import com.moxi.mogublog.utils.RedisUtil;
import com.moxi.mogublog.utils.SpringUtils;
import com.moxi.mogublog.utils.StringUtils;
import com.moxi.mougblog.base.global.Constants;
import com.moxi.mougblog.base.global.ECode;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * token拦截器
 *
 * @author: 陌溪
 * @create: 2020-06-14-21:55
 */
public class TokenInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        StringBuffer requestURL = request.getRequestURL();
        //得到请求头信息authorization信息
        String authHeader = "";

        if (request.getHeader("Authorization") != null) {
            authHeader = request.getHeader("Authorization");
            //System.out.println("head");
        } else if (request.getParameter(SysConf.TOKEN) != null) {
            authHeader = request.getParameter(SysConf.TOKEN);
            //System.out.println("para");
        }

        System.out.println("picturetoken"+request.getHeader("pictureToken"));
        if (StringUtils.isNotEmpty(authHeader) && authHeader.startsWith("bearer_")) {
            // 获取在线的管理员信息
            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
            String onlineAdmin = redisUtil.get(RedisConf.LOGIN_TOKEN_KEY + RedisConf.SEGMENTATION + authHeader);//看看这个用户在redis中是否登录
            if (StringUtils.isNotEmpty(onlineAdmin)) {
                // 得到管理员UID和 Name
                OnlineAdmin admin = JsonUtils.jsonToPojo(onlineAdmin, OnlineAdmin.class);
                request.setAttribute(SysConf.ADMIN_UID, admin.getAdminUid());
                request.setAttribute(SysConf.NAME, admin.getUserName());
                request.setAttribute(SysConf.TOKEN, authHeader);
            }else{
                //System.out.println("被拦截");
                String msg = com.baomidou.mybatisplus.core.toolkit.StringUtils.format("请求访问：{}，认证失败，无法访问系统资源", request.getRequestURI());
                response.setStatus(ECode.SUCCESS);
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");
                Map<String, Object> result = new HashMap<>(Constants.NUM_THREE);
                result.put("code", ECode.UNAUTHORIZED);
                result.put("msg", msg);
                result.put("data", "token无效或过期,请重新登录");
                response.getWriter().write(JSONObject.toJSONString(result));
                return false;
            }
        }
        return true;
    }

}

